# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=java-snappy
_pkgname=snappy-java
pkgver=1.1.8.4
_libver=${pkgver%.*}
pkgrel=0
pkgdesc="Snappy compressor/decompressor for Java"
url="https://github.com/xerial/snappy-java"
# x86 and riscv64 blocked by openjdk8
# aarch64 fails to build https://gitlab.alpinelinux.org/alpine/aports/-/issues/12925
arch="all !x86 !riscv64"
license="Apache-2.0"
options="!check" # Tests does not work correctly
depends="java-jre-headless"
makedepends="bash openjdk8 perl snappy-dev>=$_libver"
subpackages="$pkgname-native"
source="$pkgname-$pkgver.tar.gz::https://github.com/xerial/snappy-java/archive/$pkgver.tar.gz
	unbundle-snappy.patch
	fix-racing-condition-unpack.patch
	"
builddir="$srcdir/$_pkgname-$pkgver"

prepare() {
	default_prepare

	# Get rid of bundled native binaries.
	rm -r src/main/resources/org/xerial/snappy/native

	# To load libsnappyjava.so from java.library.path.
	cat > src/main/resources/org-xerial-snappy.properties <<-EOF
		org.xerial.snappy.use.systemlib=true
	EOF
}

build() {
	JAVA_HOME="/usr/lib/jvm/default-jvm" make

	msg "Downloading SBT and then building JAR (this will take few minutes)..."
	JVM_OPTS="-Xms256m -Xmx256m -Xss2m" \
		./sbt -no-share -sbt-launch-dir project/.sbt/launchers package
}

package() {
	depends="$depends $pkgname-native"

	install -m644 -D "$builddir"/target/$_pkgname-*.jar \
		"$pkgdir"/usr/share/java/$_pkgname-$pkgver.jar
	ln -sf $_pkgname-$pkgver.jar "$pkgdir"/usr/share/java/$_pkgname.jar
}

native() {
	local soname="libsnappyjava.so"

	install -m755 -D "$builddir"/target/$soname \
		"$subpkgdir"/usr/lib/$soname.$_libver
	ln -sf $soname.$_libver "$subpkgdir"/usr/lib/$soname
}

sha512sums="
94813b64acb579b9e63315aa7765f2a2989057c93745edd714d59ebe59ae778958a9bd376ba3d831c21e79bc4c5fb7f8fb969ee827e307f7fc1dbed004df010b  java-snappy-1.1.8.4.tar.gz
6f1e402435faa000e97cd18486c779be39e23d904077f79d614fd95d8feff6fe15c0292ff9dc463089f35817dcd9f4691e8248bc8caa9b8796d9b18175364af1  unbundle-snappy.patch
24c0e11b191f582f3c0e6ca14af1ef04067b4fb832097dfad82c1cacaf3f37a32c42b9a4cd04e8247188f746438cfed8ac9ed1203ad5e3b8df5aca5ea41a8aa3  fix-racing-condition-unpack.patch
"
